package com.saicmotor.room.Database;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.saicmotor.room.Dao.UserDao;
import com.saicmotor.room.Entity.User;

/**
 * @Author xinxinzhang
 * @Date 2022/08/17
 **/
@Database(entities = {User.class}, version=1, exportSchema = false)
public abstract class UserDatabase extends RoomDatabase {

    private static final String name = "UserDatabase.db";
    private static volatile UserDatabase instance;

    // 单例
    // allowMainThreadQueries()
    // 运行在主线程调用 allowMainThreadQueries()
    public static synchronized UserDatabase getInstance(Context context) {
        if (instance == null) {
            instance = Room.databaseBuilder(
                            context,
                            UserDatabase.class,
                            name)
                    .allowMainThreadQueries()
                    .build();
        }
        return instance;
    }

    public abstract UserDao getUserDao();
}
